import china from 'echarts/map/json/china.json'
import anhui from 'echarts/map/json/province/anhui.json'
import aomen from 'echarts/map/json/province/aomen.json'
import beijing from 'echarts/map/json/province/beijing.json'
import chongqing from 'echarts/map/json/province/chongqing.json'
import fujian from 'echarts/map/json/province/fujian.json'
import gansu from 'echarts/map/json/province/gansu.json'
import guangdong from 'echarts/map/json/province/guangdong.json'
import guangxi from 'echarts/map/json/province/guangxi.json'
import guizhou from 'echarts/map/json/province/guizhou.json'
import hainan from 'echarts/map/json/province/hainan.json'
import hebei from 'echarts/map/json/province/hebei.json'
import heilongjiang from 'echarts/map/json/province/heilongjiang.json'
import henan from 'echarts/map/json/province/henan.json'
import hubei from 'echarts/map/json/province/hubei.json'
import hunan from 'echarts/map/json/province/hunan.json'
import jiangsu from 'echarts/map/json/province/jiangsu.json'
import jiangxi from 'echarts/map/json/province/jiangxi.json'
import jilin from 'echarts/map/json/province/jilin.json'
import liaoning from 'echarts/map/json/province/liaoning.json'
import neimenggu from 'echarts/map/json/province/neimenggu.json'
import ningxia from 'echarts/map/json/province/ningxia.json'
import qinghai from 'echarts/map/json/province/qinghai.json'
import shandong from 'echarts/map/json/province/shandong.json'
import shanghai from 'echarts/map/json/province/shanghai.json'
import shan1xi from 'echarts/map/json/province/shanxi.json'
import shan3xi from 'echarts/map/json/province/shanxi1.json'
import sichuan from 'echarts/map/json/province/sichuan.json'
import taiwan from 'echarts/map/json/province/taiwan.json'
import tianjin from 'echarts/map/json/province/tianjin.json'
import xianggang from 'echarts/map/json/province/xianggang.json'
import xinjiang from 'echarts/map/json/province/xinjiang.json'
import xizang from 'echarts/map/json/province/xizang.json'
import yunnan from 'echarts/map/json/province/yunnan.json'
import zhejiang from 'echarts/map/json/province/zhejiang.json'

let allDistricts = [china, anhui, aomen, beijing, chongqing, fujian, gansu, guangdong, guangxi,
  guizhou, hainan, hebei, heilongjiang, henan, hubei, hunan, jiangsu, jiangxi, jilin, liaoning,
  neimenggu, ningxia, qinghai, shandong, shanghai, shan1xi, shan3xi, sichuan, tianjin,
  xianggang, xinjiang, xizang, yunnan, zhejiang]

// build 行政区划代码 - 行政区名称的 map
allDistricts = allDistricts.flatMap(district => district.features)
const chinaAdCodeToDistrictNameMap = {}
allDistricts.forEach(district => {
  chinaAdCodeToDistrictNameMap[district.id] = district.properties.name
})

// 台灣省数据结构与其他省市不同，需要单独处理
const taiwanCity = taiwan.features
taiwanCity.forEach(city => {
  const properties = city.properties
  if (properties.woe_id) {
    chinaAdCodeToDistrictNameMap[properties.woe_id] = properties.name
  }
})

// build 行政区名称 - 行政区划代码的 map
const districtNameToChinaAdCodeMap = {}
Object.entries(chinaAdCodeToDistrictNameMap).forEach(([key, value]) => {
  districtNameToChinaAdCodeMap[value] = key
})

// build 行政区划代码 - 行政区 GeoJSON 的 map
const chinaAdCodeToGeoJsonMap = {
  '710000': taiwan,
  '130000': hebei,
  '140000': shan1xi,
  '150000': neimenggu,
  '210000': liaoning,
  '220000': jilin,
  '230000': heilongjiang,
  '320000': jiangsu,
  '330000': zhejiang,
  '340000': anhui,
  '350000': fujian,
  '360000': jiangxi,
  '370000': shandong,
  '410000': henan,
  '420000': hubei,
  '430000': hunan,
  '440000': guangdong,
  '450000': guangxi,
  '460000': hainan,
  '510000': sichuan,
  '520000': guizhou,
  '530000': yunnan,
  '540000': xizang,
  '610000': shan3xi,
  '620000': gansu,
  '630000': qinghai,
  '640000': ningxia,
  '650000': xinjiang,
  '110000': beijing,
  '120000': tianjin,
  '310000': shanghai,
  '500000': chongqing,
  '810000': xianggang,
  '820000': aomen
}

/**
 * 中国行政区划代码 ➡️️ 行政区名称
 *
 * @param {string} adCode 中国行政区划代码
 * @returns {string} 行政区名称
 */
export function mapChinaAdCodeToDistrictName (adCode) {
  return chinaAdCodeToDistrictNameMap[adCode]
}

/**
 * 中国行政区名称 ➡️️ 行政区划代码
 *
 * @param {string} name 中国行政区名称
 * @returns {string} 行政区划代码
 */
export function mapDistrictNameToChinaAdCode (name) {
  return districtNameToChinaAdCodeMap[name]
}

/**
 * 中国行政区划代码 ➡️️ 行政区 GeoJSON
 * @param {string} adCode 中国行政区划代码
 * @returns {Object} 行政区 GeoJSON
 */
export function mapChinaAdCodeToGeoJson (adCode) {
  return chinaAdCodeToGeoJsonMap[adCode]
}

export const zipCodeToProvinceCodeObj = {}
